package com.seadragon.app.ksr.service.impl;

import static com.seadragon.app.ksr.model.KsrConstants.KSR_INDEX_TYPE;
import static com.seadragon.app.ksr.model.KsrConstants.OFFICE_SEARCH_TYPE;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seadragon.app.ksr.dao.OfficeDao;
import com.seadragon.app.ksr.model.Address;
import com.seadragon.app.ksr.model.Business;
import com.seadragon.app.ksr.model.Office;
import com.seadragon.app.ksr.service.AddressService;
import com.seadragon.app.ksr.service.GeocodeService;
import com.seadragon.app.ksr.service.OfficeService;
import com.seadragon.app.ksr.service.ReviewService;
import com.seadragon.app.ksr.service.SearchService;
import com.seadragon.app.ksr.util.IndexManager;

@Service("officeService")
@Transactional
public class OfficeServiceImpl implements OfficeService {
	private Logger logger = LoggerFactory.getLogger(OfficeServiceImpl.class);
	@Autowired
	private OfficeDao officeDao;

	@Autowired
	private IndexManager indexManager;

	@Autowired
	private SearchService searchService;

	@Autowired
	private AddressService addressService;

	@Autowired
	private ReviewService reviewService;

	@Autowired
	private GeocodeService geocodeService;

	@Override
	public Office findById(long id) {
		return officeDao.findById(id);
	}

	@Override
	public Office update(Office office) {
		Address address = addressService.save(office.getAddress());
		office.setAddress(address);

		try {
			indexManager.insertDocument(office);
			return officeDao.update(office);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
		return null;
	}

	@Override
	public List<Office> findAll(int offset, int pageSize) {
		return officeDao.findAll(offset, pageSize);
	}

	@Override
	public List<Office> findAllBy(Business business) {
		return officeDao.findAllBy(business);
	}

	@Override
	public void save(Office office) {
		Address address = addressService.save(office.getAddress());
		office.setAddress(address);

		try {
			indexManager.insertDocument(office);
			officeDao.save(office);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
	}

	@Override
	public boolean remove(long id) {
		Office office = new Office();
		office.setId(id);
		return remove(office);
	}

	@Override
	public boolean remove(Office office) {
		long reviewCount = reviewService.count(office);
		if(reviewCount > 0){
			return false;
		}else{
			try {
				indexManager.deleteDocument(KSR_INDEX_TYPE, OFFICE_SEARCH_TYPE, office.getId());
				officeDao.delete(office);
				return true;
			} catch (Exception ex) {
				logger.error(ex.getMessage(), ex);
			}
		}
		return false;
	}

	@Override
	public List<Office> findAllBy(Business business, int offset, int pageSize) {
		return officeDao.findAllBy(business, offset, pageSize);
	}
}
